package projectManagementServer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateUserGUI extends JPanel implements ActionListener{
	private JTextField textUserName;
	private JTextField textPassword;
	private JTextField textStatus;
	private JFrame f;
	private JButton btnDeactivate;
	
	public UpdateUserGUI(Member mem){
		setPreferredSize(new Dimension(368, 243));
		setLayout(null);
		
		textUserName = new JTextField();
		textUserName.setColumns(10);
		textUserName.setBounds(66, 47, 226, 35);
		textUserName.setText(mem.getUserName());
		add(textUserName);
		
		
		textPassword = new JTextField();
		textPassword.setColumns(10);
		textPassword.setBounds(66, 103, 226, 35);
		textPassword.setText(mem.getPassword());
		add(textPassword);
		
		
		JLabel lblUsername = new JLabel("UserName:");
		lblUsername.setBounds(66, 34, 226, 14);
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(66, 90, 226, 14);
		add(lblPassword);
		
		JLabel lblStatus = new JLabel("Status: ");
		lblStatus.setBounds(66, 147, 46, 14);
		add(lblStatus);
		
		textStatus = new JTextField();
		textStatus.setColumns(10);
		textStatus.setEditable(false);
		textStatus.setBounds(66, 160, 110, 25);
		textStatus.setText(mem.getStatus());
		add(textStatus);
		
		
		btnDeactivate = new JButton("DEACTIVATE");
		btnDeactivate.setBounds(186, 160, 106, 25);
		btnDeactivate.putClientProperty("Quaqua.Button.style", "bevel");
		btnDeactivate.addActionListener(this);
		add(btnDeactivate);
		
		JButton btnOk = new JButton("OK");
		btnOk.setBounds(66, 196, 115, 35);
		btnOk.putClientProperty("Quaqua.Button.style", "bevel");
		btnOk.addActionListener(this);
		add(btnOk);
		
		JButton btnCancel = new JButton("CANCEL");
		btnCancel.setBounds(178, 196, 115, 35);
		btnCancel.putClientProperty("Quaqua.Button.style", "bevel");
		btnCancel.addActionListener(this);
		add(btnCancel);
		
		if(mem.getStatus().equalsIgnoreCase("DEACTIVATE")){
			btnDeactivate.setText("ACTIVATE");
			btnDeactivate.setActionCommand("ACTIVATE");
		}
		
		f = new JFrame();
		f.getContentPane().add(this);
		f.setVisible(true);
		f.setResizable(false);
		f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		f.pack();
		f.setLocationRelativeTo(null);
	}

	@Override
	public void actionPerformed(ActionEvent btn) {
		String cmd = btn.getActionCommand();
		
		if(cmd.equalsIgnoreCase("OK")){
			try{
				Member mem = new Member(textUserName.getText(), textPassword.getText(), textStatus.getText(), "");
				AccessDatabase db = new AccessDatabase();
				db.updateMember(mem);
				ServerGUI.updateMember(mem);
				f.setVisible(false);
			}catch(Exception e){
				e.printStackTrace();
			}
			
		}
		if(cmd.equalsIgnoreCase("CANCEL")){
			f.setVisible(false);
		}
		if(cmd.equalsIgnoreCase("DEACTIVATE")){
			textStatus.setText("DEACTIVATE");
			btnDeactivate.setText("ACTIVATE");
		}
	}

}
